package xyz.jcat.webflux.sucurity;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.security.web.server.authentication.logout.ServerLogoutSuccessHandler;
import reactor.core.publisher.Mono;
import xyz.jcat.auth.token.AccessTokenUtils;
import xyz.jcat.common.auth.UserContextHolder;
import xyz.jcat.common.web.Resp;

public class DefaultServerLogoutSuccessHandler implements ServerLogoutSuccessHandler {

    @Override
    public Mono<Void> onLogoutSuccess(WebFilterExchange exchange, Authentication authentication) {
        String requestHeaderToken = exchange.getExchange().getRequest()
                .getHeaders().getFirst(AccessTokenUtils.TOKEN_HEADER);
        String accessToken = AccessTokenUtils.getHeaderAccessToken(requestHeaderToken);
        AccessTokenUtils.removeAccessToken(UserContextHolder.getInstance().getUserId(), accessToken);
        return ServerUtils.writeJson(exchange.getExchange(), Resp.ok("退出登录成功"));
    }
}
